<?php
/**
 * Firal CMS
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to firal-dev@googlegroups.com so we can send you a copy immediately.
 *
 * @category   Firal
 * @package    Firal_Controller
 * @subpackage Action_Helper
 * @copyright  Copyright (c) 2008 MaakSite.nl Productions. (http://productions.maaksite.nl/)
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 * @version    $Id: ThemeManager.php 7 2008-05-14 15:25:55Z pieterkokx $
 */

/**
 * @see Zend_Controller_Action_Helper_Abstract
 */
require_once 'Zend/Controller/Action/Helper/Abstract.php';

/**
 * @see Zend_View_Interface
 */
require_once 'Zend/View/Interface.php';

/**
 * @see Zend_View
 */
require_once 'Zend/View.php';

/**
 * Theme manager for the Firal CMS
 *
 * @uses       Zend_Controller_Action_Helper_Abstract
 * @category   Firal
 * @package    Firal_Controller
 * @subpackage Action_Helper
 * @copyright  Copyright (c) 2008 MaakSite.nl Productions. (http://productions.maaksite.nl/)
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 */
class Firal_Controller_Action_Helper_ThemeManager extends Zend_Controller_Action_Helper_Abstract
{
    
    /**
     * The basic theme apth
     * 
     * @var string
     */
    protected $_themePath = '.';
    
    /**
     * The view path specification
     * 
     * @var string
     */
    protected $_viewPathSpec = ':theme/:module';
    
    /**
     * The current theme
     * 
     * @var string
     */
    protected $_theme;
    
    /**
     * The Zend_View instance
     * 
     * @var Zend_View_Interface
     */
    protected $_view;
    
    
    /**
     * Constructor function
     * 
     * @param string $theme
     * 
     * @return void
     */
    public function __construct($theme = null)
    {
        $this->setTheme($theme);
    }
    
    /**
     * Get current module name
     * 
     * @return string
     */
    public function getModule()
    {
        $module  = $this->getRequest()->getModuleName();
        
        if (null === $module) {
            $module = $this->getFrontController()->getDispatcher()->getDefaultModule();
        }

        return $module;
    }
    
    /**
     * Set the view
     * 
     * @param Zend_View_Interface $view
     * 
     * @return Firal_Controller_Action_Helper_ThemeManager
     */
    public function setView(Zend_View_Interface $view)
    {
        $this->_view = $view;
        
        return $this;
    }
    
    /**
     * Set the theme path
     * 
     * @param string $path
     * 
     * @return Firal_Controller_Action_Helper_ThemeManager
     */
    public function setThemePath($path)
    {
        $this->_themePath = $path;
        
        return $this;
    }
    
    /**
     * Set the viewPath specification
     * 
     * @param string $spec
     * 
     * @return Firal_Controller_Action_Helper_ThemeManager
     */
    public function setViewPathSpec($spec = ':theme/:module')
    {
        $this->_viewPathSpec = $spec;
        
        return $this;
    }
    
    /**
     * Set the theme
     * 
     * @param string $theme
     * 
     * @return Firal_Controller_Action_Helper_ThemeManager
     */
    public function setTheme($theme)
    {
        $this->_theme = $theme;
        
        return $this;
    }
    
    /**
     * Get the current view
     * 
     * @return Zend_View_Interface
     */
    public function getView()
    {
        if (null === $this->_view) {
            if (isset($this->getActionController()->view)
            && ($this->getActionController()->view instanceof Zend_View_Interface)) {
                $this->_view = $this->getActionController()->view;
            }
        }
        
        return $this->_view;
    }
    
    /**
     * Get the base path
     * 
     * @return string
     */
    protected function _getBasePath($theme, $module)
    {
        $path = $this->_viewPathSpec;
        
        $path = str_replace(':theme', $theme, $path);
        $path = str_replace(':module', $module, $path);
        
        return $path;
    }
    
    /**
     * PostDispatch action hook
     * 
     * @return void
     */
    public function postDispatch()
    {
        $path = $this->_themePath;
        
        $path .= '/' . $this->_getBasePath($this->_theme, $this->getModule());
        
        $this->getView()->addBasePath($path);
    }
}